"""
调用远程django api
"""
import requests


class DjangoProcessConfig:
    def __init__(self):
        self.url_process_config = f"http://49.232.195.113:8000/mainapp/process_config"
        self.process_config_json = self.get_process_config_json()

    def get_process_config_json(self):
        """pass"""
        data = requests.get(self.url_process_config).json()
        return data

    def get_device_names(self):
        self.process_config_json = self.get_process_config_json()
        datas = sorted(self.process_config_json["device_config"], key=lambda x: x["process_number"])
        return [x["device_name"] for x in datas]

    def get_process_info(self, device_name):
        """获取设备对应的工序号信息"""
        self.process_config_json = self.get_process_config_json()
        for device_config in self.process_config_json["device_config"]:
            if device_config.get("device_name") == device_name:
                return device_config.get("process_number"), device_config.get("process_name")
        return None, None

    def query_product_type(self, log_number):
        self.process_config_json = self.get_process_config_json()
        for device_config in self.process_config_json["process_database"]:
            if str(device_config.get("LotNumber")) == str(log_number):
                return str(device_config.get("ProductType"))
        return None


if __name__ == '__main__':
    obj = DjangoProcessConfig()
    print(obj.get_device_names())
